//
//  XSGrid.m
//  Test
//
//  Created by xueshan1 on 2018/12/17.
//  Copyright © 2018 xueshan1. All rights reserved.
//

#import "XSGrid.h"
#import "NSString+Draw.h"
#import "XSGridRow.h"


@implementation XSGrid

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _lineWidth = 0.6;
        _rowHeight = 20;
        _fontSize = 14;
        _lineColor = [UIColor blackColor];
        _textColor = [UIColor blackColor];
        
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, _lineWidth);
    CGContextSetFillColorWithColor(context, _lineColor.CGColor);
    
    //画header
    CGFloat header_Hei = _rowHeight;
    [self drawRowHeaderWithContext:context rect:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, header_Hei)];
    //画每一行
    [self drawRowDatasWithContext:context rect:CGRectMake(rect.origin.x, header_Hei, rect.size.width, rect.size.height - header_Hei)];
    
 
}
//header
- (void)drawRowHeaderWithContext:(CGContextRef)context rect:(CGRect)rect{
    //上线
    CGContextFillRect(context, CGRectMake(0, 0, rect.size.width, _lineWidth));
    
    for (int i = 0; i<self.columnDatas.count; i++) {
        CGContextSaveGState(context);

        XSGridColumn *column = self.columnDatas[i];
        //左线
        if (!column.mergeLeft) {
            CGContextFillRect(context, CGRectMake(column.columnWidth * i, 0, _lineWidth, _rowHeight));
        }
        
        CGRect titleRect = CGRectMake(column.columnWidth * i, rect.origin.y, column.columnWidth, rect.size.height);

        //画title
        [column.title xs_drawWithContext:context rect:titleRect fontSize:15 lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter color:_textColor];
        CGContextRestoreGState(context);
    }
    
    //右线
    CGContextFillRect(context, CGRectMake(rect.size.width - _lineWidth, rect.origin.y, _lineWidth, _rowHeight));

    //下线
    CGContextFillRect(context, CGRectMake(0, _rowHeight, rect.size.width, _lineWidth));
}

//行
- (void)drawRowDatasWithContext:(CGContextRef)context rect:(CGRect)rect{
    CGFloat rowY = rect.origin.y;
    
    for (XSGridRow *row in self.rowDatas) {
        //上线
        CGContextFillRect(context, CGRectMake(0, rowY, rect.size.width, _lineWidth));
        //取一行中 字数最多的
        NSString *lengestStr = [self getRowLengestString:row.cells];

        //每一行的每一个cell(每一列的数据)
        CGFloat cellX = rect.origin.x;
        for (XSGridCell *cell in row.cells) {
            CGContextSaveGState(context);
 
            CGSize innerSize = CGSizeMake(cell.width - cell.contentPadding.left - cell.contentPadding.right, row.rowHeight - cell.contentPadding.top - cell.contentPadding.bottom);
            //最高度
            CGFloat cell_Max_Height = [lengestStr xs_sizeWithConstrainedSize:CGSizeMake(innerSize.width, 1000) fontSize:_fontSize lineBreadMode:NSLineBreakByWordWrapping].height;
            
            row.rowHeight = MAX(row.rowHeight, cell_Max_Height + cell.contentPadding.top + cell.contentPadding.bottom);
            
            if (!cell.mergeLeft) {
                //左线
                CGContextFillRect(context, CGRectMake(cellX, rowY, _lineWidth, row.rowHeight));
            }
            if (cell.title.length > 0) {
                //cell中文字实际高度
                CGFloat titleHeight = [cell.title xs_sizeWithConstrainedSize:CGSizeMake(innerSize.width, 1000) fontSize:_fontSize lineBreadMode:NSLineBreakByWordWrapping].height;
                
                CGRect titleRect = CGRectMake(cellX + cell.contentPadding.left , rowY + (row.rowHeight -  titleHeight - cell.contentPadding.top - cell.contentPadding.bottom)/2, innerSize.width, row.rowHeight);
                
                [cell.title xs_drawWithContext:context rect:titleRect fontSize:_fontSize lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter color:_textColor];

            }
            //画自定制的view
            if (cell.image && [cell.image isKindOfClass:[UIImage class]]) {
                CGFloat imageWidth = cell.image.size.width;
                CGFloat imageHeight = cell.image.size.height;
                CGRect viewRect = CGRectMake(cellX + (cell.width - imageWidth)/2 + cell.contentPadding.left  , rowY + (row.rowHeight - imageHeight - cell.contentPadding.top - cell.contentPadding.bottom)/2, cell.image.size.width, imageHeight);
                
                [cell.image drawInRect:viewRect];
                
            }
            
            
            CGContextRestoreGState(context);

            cellX += cell.width;
        }
        //右线
        CGContextFillRect(context, CGRectMake(cellX - _lineWidth, rowY, _lineWidth, row.rowHeight));
        
        rowY += row.rowHeight;
    }
    
    //下线
    CGContextFillRect(context, CGRectMake(rect.origin.x, rowY, rect.size.width, _lineWidth));
    
}

- (NSString *)getRowLengestString:(NSArray<XSGridCell *>*)cells{
    NSString *lengestStr = cells.firstObject.title;
    for (XSGridCell *cell in cells) {
        if (lengestStr.length < cell.title.length) {
            lengestStr = cell.title;
        }
    }
    return lengestStr;
}

- (void)refreshGrid{
    
    [self setNeedsDisplay];
}
#pragma mark - 添加数据
- (void)addColumn:(XSGridColumn *)column{
    if (!_columnDatas) {
        _columnDatas = [NSMutableArray array];
    }
    
    [_columnDatas addObject:column];
}

- (void)addRow:(NSDictionary *)dic{
    [self addRow:dic rowhHeight:_rowHeight];
}

- (void)addRow:(NSDictionary *)dic rowhHeight:(CGFloat)rowHeight{
    if (!_rowDatas) {
        _rowDatas = [NSMutableArray array];
    }
    
    XSGridRow *row = [[XSGridRow alloc]init];
    row.rowHeight = rowHeight;
    NSMutableArray *cells = [NSMutableArray array];
    for (XSGridColumn *column in self.columnDatas) {
        XSGridCell *cell = [column gridCellWithData:dic];
        [cells addObject:cell];
    }
    row.cells = cells;
    
    [_rowDatas addObject:row];
    
}

@end
